package com.atguigu.handler;

import com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
import org.springframework.web.bind.annotation.*;
import sun.awt.SunHints;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author 琉
 * @create 2021-09-05 18:42
 */
@Controller
public class Demo01Handler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ServletContext servletContext;


    @RequestMapping("/test")
    public  String showPortal(){
        logger.debug("showPortal()方法被调用了");
        return "index";
    }

    //目标页面的方法
    @RequestMapping("/toTarget")
    public String target() {
        logger.debug("Target方法被调用");
        return "target";
    }

    //模糊匹配
    @RequestMapping("/animal/**")
    public String testRequestMappingMatch() {

        logger.debug("测试 @RequestMapping 注解的模糊匹配");

        return "target";
    }

    @RequestMapping("/getHeader")
    public String getHeaderValue(@RequestHeader("user-agent") String headerValue){
        logger.debug("头的值是:"+headerValue);
        return "target";
    }

    @RequestMapping("/getCookie")
    public String getCookieVALUE(@CookieValue(value = "JSESSIONID",defaultValue = "abc") String cookieValue){
        logger.debug("cookie值是:"+cookieValue);
        return "target";

    }

    @GetMapping("/emp")
    public String empGet(){
        logger.debug("这是empGet()方法");
        return "target";
    }


    @RequestMapping(value = "/emp2", method = RequestMethod.GET)
    @PostMapping("/emp2")
    public String empPost(){
        logger.debug("这是empGet()方法");
        return "target";

    }


    //使用 ModelMap 类型的形参将数据存入请求域对象
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
            map.put("username","老周");
            return "target";

    }

    //访问会话域
    @RequestMapping("/session")
    public String attrSession(HttpSession session){
       session.setAttribute("youName","老邓");

        return "target";
    }

    //使用 Model 类型的形参操作将数据存入请求域对象
    @RequestMapping("/model")
    public String testModel(Model model){
        model.addAttribute("username","老余");
        return  "target";
    }

    //访问应用域
    @RequestMapping("/application")
    public String attrApplication(){
        servletContext.setAttribute("appApplication","老张");
        return "target";
    }

}
